<script lang="ts" setup>
import { ref, onMounted } from "vue";
import router from "@/router";
import { useRoute } from "vue-router";
import applyForm from "./components/applyForm.vue";
import PageWrapper from "@/components/Wrapper";
import detailTable from "./components/detailTable.vue";
import operation from "./components/operation.vue";
import { setWarehouseAllocationAdd } from "@/api/warehouseAllocation";
import { add } from "@/utils/caculate";
import { useMessage } from "@/hooks/useMessage";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";

const multiTagsStore = useMultiTagsStoreHook();
const route = useRoute();

defineOptions({
  name: "inventoryTransferAddDetail"
});
const tableData = ref([]);
const detailTableRef = ref();
const tableHeight = ref(0);
const pageWrapperRef = ref();
const applyFormRef = ref(null);
const inboundType = ref(1);
const preWarehouseCode = ref(""); // 上一次选择的仓库
const rows = ref(0);
const total = ref(0);
const { createNotification } = useMessage();

onMounted(() => {
  pageWrapperRef.value.initData();
});

const submitApply = () => {
  applyFormRef.value.validateForm().then(() => {
    detailTableRef.value
      .validateForm()
      .then(() => {
        setWarehouseAllocationAdd(
          Object.assign(applyFormRef.value.getForm(), {
            itemList: [...detailTableRef.value.getTableData()]
          })
        ).then(res => {
          if (res.code === 0) {
            createNotification({
              title: "温馨提示",
              message: res.msg,
              type: "success"
            });
            multiTagsStore.handleTags("splice", route.path);
            router.back();
          }
        });
      })
      .catch(err => {
        createNotification({
          type: "error",
          message: "请输入调出数量"
        });
      });
  });
};

// 设置表格行数和出库物料总数
const setTotalData = (data: any) => {
  rows.value = data.length;
  total.value = data.reduce((acc: number, curr: any) => add(acc, curr.num), 0);
};
</script>
<template>
  <div class="main_box">
    <PageWrapper
      ref="pageWrapperRef"
      v-model:height="tableHeight"
      :isShowBottom="false"
      :isShowLine="false"
    >
      <template #header>
        <applyForm
          ref="applyFormRef"
          v-model:inboundType="inboundType"
          v-model:preWarehouseCode="preWarehouseCode"
          :rows="rows"
        />
      </template>
      <template #content>
        <div class="table_box">
          <operation />
          <detailTable
            ref="detailTableRef"
            :list="tableData"
            :preWarehouseCode="preWarehouseCode"
            :tableHeight="tableHeight"
            :inboundType="inboundType"
            @setTotalData="setTotalData"
          />
        </div>
      </template>
      <template #footer>
        <section
          class="flex flex-row justify-between pb-[16px] pt-[16px] pr-[24px] pl-[24px] bg-white rounded-t-[8px]"
        >
          <div class="flex flex-row">
            <div class="flex flex-row items-center mr-[80px]">
              <span>调拨出库物料记录行数：</span>
              <p class="text-[20px] font-bold">{{ rows }}</p>
            </div>
            <div class="flex flex-row items-center">
              <span>调拨出库物料总数：</span>
              <p class="text-[20px] font-bold">{{ total }}</p>
            </div>
          </div>
          <el-button type="primary" @click="submitApply"
            ><span class="text-[16px]">提交</span></el-button
          >
        </section>
      </template>
    </PageWrapper>
  </div>
</template>
<style lang="scss" scoped></style>
